package com.accp.action;
import com.accp.biz.PersonBiz;
import com.accp.pojo.Person;
import com.accp.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class PersonAction implements ModelDriven<Person> {
    private Person person=new Person();//接受参数
    @Override
    public Person getModel(){return person; }
    private PersonBiz personBiz;
    public void setPersonBiz(PersonBiz personBiz) {
        this.personBiz = personBiz;
    }

    /**
     * 显示所有员工
     * @return
     */
  /*  public String getAllPerson() {
        Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        try {
            List list = personBiz.getAll("Person");
            request.put("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "getAllPerson";
    }*/
    /**
     * 调转到修改员工信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public String toupdate() {
        int pid = Integer.parseInt(ServletActionContext.getRequest().getParameter("pid"));
        Person person=personBiz.findById(pid);
        Map<String,Object> session = ActionContext.getContext().getSession();//获取seesion作用域对象。
        session.put("person", person);
        return "toupdate";
    }
    /**
     * 修改员工信息
     * @return
     */
    //@SuppressWarnings("unchecked")
    public String update(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        Person p = (Person) session.get("person");
        p.setPname(person.getPname());
        p.setPsex(person.getPsex());
        p.setPtel(person.getPtel());
        boolean flag = personBiz.update(p);
        if(flag){
            request.put("message", "修改成功");
            return "update";
        }
        return "error";
    }

    /**
     * 根据ID删除员工
     * @param
     * @return
     */
    public String delById() {
        int pid = Integer.parseInt(ServletActionContext.getRequest().getParameter("pid"));
       if (personBiz.delById(pid)){
        return "delById";
    }
    return "error";
    }

    /**
     * 根据对象删除员工
     * @param
     * @return
     */
    public String deletePerson() {
        int pid = Integer.parseInt(ServletActionContext.getRequest().getParameter("pid"));
        Person person=personBiz.findById(pid);
        if (personBiz.deletePerson(person)){
            return "deletePerson";
        }
        return "error";
    }
    /**
     * 添加员工
     * @param
     * @return
     */
    //@SuppressWarnings("unchecked")
    public String add() {
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        boolean flag = personBiz.add(person);
        if(flag){
            request.put("message", "添加成功");
            return "add";
        }
        return "error";
    }
    /**
     * 分页查看用户信息
     *
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String selectFirstPage(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        try{
            Map<String,Object> session =  ActionContext.getContext().getSession();
          /*  Person person=(Person) session.get("person");
            String pname=person.getPname();*/
            List list=personBiz.selectByPage("Person",1,4);
            int totalPage=personBiz.searchRecordsNextPage(3,"Person");
            request.put("list", list);
            request.put("totalPage", totalPage);
            request.put("currentPage", 1);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectFirstPage";
    }
    /**
     * 分页查找下一页
     * @return
     */
    @SuppressWarnings("rawtypes")
    public String selectNextPage(){
        HttpServletRequest request = ServletActionContext.getRequest();
        int next = Integer.parseInt(request.getParameter("page"));
        try{
          //  Map<String,Object> session =  ActionContext.getContext().getSession();
            //Users user = (Users) session.get("user");
          //  Person person=(Person) session.get("person");
            //String userName = user.getUserName();
           // String pname=person.getPname();
            //List list = usersBiz.selectByPage("Users", next, 3,userName);
            List list=personBiz.selectByPage("Person",next,4);
            //int total = usersBiz.searchRecordsNextPage(3, "Users",userName);
            int total=personBiz.searchRecordsNextPage(3,"Person");
            request.setAttribute("list", list);
            request.setAttribute("currentPage", next);
            request.setAttribute("totalPage", total);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectNextPage";
    }
}
